import EventManager from "../../../../scripts/Common/EventManager";
import Loader from "../../../../scripts/Common/Loader";
import yyComponent from "../../../../scripts/Common/yyComponent";
import { EventType } from "../../../../scripts/GameSpecial/GameEventType";
import { sers, mgrs } from "../../../../scripts/frameSccript/system/OverallSystem ";
import { GameDataHelp } from "../../../../scripts/frameSccript/system/gameDataHelp";
import UserService from "../../services/userService";

const { ccclass, property } = cc._decorator;

@ccclass
export default class gameListItemNew extends yyComponent {



    // onLoad () {}

    @property(cc.Sprite)
    icon: cc.Sprite = null

    @property(cc.Node)
    vicon: cc.Node = null

    @property(cc.Label)
    gameNmae: cc.Label = null

    @property(cc.Label)
    tips: cc.Label = null

    @property(cc.Node)
    lockNode: cc.Node = null

    @property(cc.Material)
    huise: cc.Material = null

    @property(cc.Material)
    zhengchang: cc.Material = null

    playGradeName: string[] =
        [
            "乞丐",
            "书童",
            "书生",
            "秀才",
            "举人",
            "进士",
            "探花",
            "榜眼",
            "状元",
            "知县",
            "博士",
            "知州",
            "知府",
            "中书令",//13
            "巡抚",
            "丞相",
            "驸马",
            "皇帝",
            "增福神",
            "财神",
            "天将",
            "东帝",
            "天王",//22
            "神君",
            "星君",
            "天尊",
            "东王公",
            "玉帝"//27
        ];

    start() {
        // this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)

        this.on(EventType.UIEvent.updataGamelist, this.updataView, this)
    }
    data = null
    setData(data) {
        cc.log(data)
        this.data = data
        this.updataView()
    }

    updataView() {
        if (!this.data) return
        let data = this.data
        this.gameNmae.string = data.name
        let playGrade = 1;
        if (+data.lockLevel < +playGrade) {
            // this.vicon.active = false
            this.vicon.children[0].active = true;
            this.vicon.children[1].active = false;
            this.tips.string = ''
            this.tips.node.active = false
            this.lockNode.active = true
        } else {
            // this.vicon.active = true
            this.vicon.children[0].active = false;
            this.vicon.children[1].active = true;
            this.tips.string = `需达到${this.playGradeName[data.lockLevel]}解锁`
            this.lockNode.active = false
        }

        let s = sers().userSrv.getTiyanGame()
        if (s.indexOf(data.lockName) >= 0 || Number(sers().localStorageSrv.get(UserService.changwanModel)) == 1) {
            // this.vicon.active = false
            this.vicon.children[0].active = true;
            this.vicon.children[1].active = false;
            this.tips.node.active = false
            this.lockNode.active = true
        }

        let m = this.lockNode.active ? this.zhengchang : this.huise

        this.icon.setMaterial(0, m)
        this.icon.node.children[0].getComponent(cc.Sprite).setMaterial(0, m)

        Loader.loadBundleRes('mainScene', 'icon/' + this.data.icon, (res) => {
            //    cc.log(res)
            if(!this || !this.node)return
            this.icon.spriteFrame = new cc.SpriteFrame(res)
        })
    }

    onTouchStart() {
        mgrs().soundMgr.playBtnSound();

        if (this.vicon.children[1].active) {
            sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
                let s = sers().userSrv.getTiyanGame()
                s.push(this.data.lockName)
                sers().userSrv.setTiyanGame(s)
                EventManager.emit(EventType.UIEvent.updataGamelist)
            }, (err) => {
            }, null, {
                game_name: '主场景更多游戏',
                prop_name: '解锁',
                level: this.data.gameType,
                slevel: this.data.name,
            },'common_video');
        } else {

            if(this.data.gameType!=10){
                if(sers().userSrv.power<=0){
                    sers().uiSrv.showUserGetPowerPage();
                    return
                }
                sers().userSrv.addPower(-1)
    
                sers().userSrv.setGameList(this.data.gameType)
                mgrs().gameMgr.enterMode(this.data.gameType);
            }else{
                sers().uiSrv.showModeSelectPage()
            }

           
        }
    }

    // update (dt) {}
}
